<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\WmActive;
use yii\data\ArrayDataProvider;

/**
 * WmActiveSearch represents the model behind the search form about `app\models\WmActive`.
 */
class WmActiveSearch extends WmActive
{

    public $fromdate;
    public $todate;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'pid', 'num', 'created_at'], 'integer'],
            [['times'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = WmActive::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (isset($params['WmActiveSearch']['fromdate']) && isset($params['WmActiveSearch']['todate'])) {
            $this->fromdate = $params['WmActiveSearch']['fromdate'];
            $this->todate = $params['WmActiveSearch']['todate'];
        }

        if(empty($_GET['sort'])){
            $_GET['sort'] = '-times';
        }
        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }


        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
//            'times' => $this->times,
            'pid' => $this->pid,
            'num' => $this->num,
//            'created_at' => $this->created_at,
        ]);

        if (Yii::$app->user->identity->is_admin == 0) {
//            $query->andFilterWhere([
//                'uid' => Yii::$app->user->id,
//            ]);
        }

        if ($this->fromdate != '' && $this->todate != '') {
            $query->andFilterWhere(['between', 'times', ($this->fromdate), ($this->todate)]);
        }

        /**
         * 1.默认显示当月数据
         * 2.搜索后显示时间段数据
         */

//        //取出的数据
//        $result  = $dataProvider->getModels();
//        //本月第几天
//        $d = 5;//date("d");
//        $nowDate = date("Y-m-");
//        for($i=0;$i<$d;$i++){
//            $data[$i]['id']=$i;
//            $s=$i+1;
//            $day = $i<9?'0'.$s:$s;
//            $data[$i]['times']=$nowDate.$day;
//            $data[$i]['num']=0;
//            $data[$i]['created_at']=$nowDate.$day;
//            $s = isset($result[$i]['times'])?$result[$i]['times']:0;
//            if(isset($result[$i]['times']) && $data[$i]['times']==$result[$i]['times']){
//                $data[$i]['times']=$result[$i]['times'];
//                $data[$i]['num']=$result[$i]['num'];
//                $data[$i]['created_at']=$result[$i]['created_at'];
//            }
//
//        }

        /**
         * 时间截实现法
         */

        $nowTime = empty($this->todate) ? strtotime(date('Y-m-d 00:00:00')) : strtotime($this->todate);
        //上月最后一天
        $pDay = empty($this->fromdate) ? strtotime(date('Y-m-t', strtotime('-1 month'))) : strtotime($this->fromdate);
//        $pDay = empty($this->fromdate) ? strtotime(date('Y-m-01',strtotime(date("Y-m-d")))): strtotime($this->fromdate);
//        $BeginDate=date('Y-m-01', strtotime(date("Y-m-d")));

        //计算这个月已过的天数
        $surplus = $nowTime - $pDay;
        $surplus = $surplus / 86400;

        $pid = isset($params['WmActiveSearch']['pid'])?$params['WmActiveSearch']['pid']:0;

        $nowTime = date("Y-m-d", $pDay + 86400);
        $result = $dataProvider->getModels();
        for ($i = 0; $i < $surplus; $i++) {
            $data[$i]['id'] = $i+1;
            $day = $pDay + 86400 * ($i+1 );
            $data[$i]['times'] = date("Y-m-d", $day);
            $data[$i]['num'] = 0;
            $data[$i]['pid'] = $pid;
            $data[$i]['created_at'] = date("Y-m-d", $day);
            $data[$i]['empty'] = true;

            if ($row = $this->_getDiffData($data[$i]['times'], $result)) {
                $data[$i]['id'] = $row['id'];
                $data[$i]['times'] = $row['times'];
                $data[$i]['num'] = $row['num'];
                $data[$i]['pid'] = $row['pid'];
                $data[$i]['created_at'] = date("Y-m-d",$row['created_at']);
                $data[$i]['empty'] = false;
            }
        }


        $provider = new ArrayDataProvider([
            'allModels' => $data,
            'pagination' => [
                'pageSize' => 20,
            ],
            'sort' => [
                'attributes' => [
//                    'times',
                    'times' => ['times' => SORT_ASC],'default' => SORT_ASC],
            ],
        ]);
        return $provider;

        return $dataProvider;
    }

    private function _getDiffData($time, $result)
    {
        $row = array();
        foreach ($result as $value) {
            $row = $value['times'] == $time ? $value : 0;
            if ($row) {
                break;
            }
        }
        return $row;
    }
}
